package com.im.partchat.db.dao;

import com.im.partchat.bean.Group;
import com.im.partchat.bean.Group_Table;
import com.im.partchat.db.PartChatDataBase;
import com.raizlabs.android.dbflow.config.DatabaseDefinition;
import com.raizlabs.android.dbflow.config.FlowManager;
import com.raizlabs.android.dbflow.sql.language.Delete;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.raizlabs.android.dbflow.structure.database.transaction.FastStoreModelTransaction;
import com.raizlabs.android.dbflow.structure.database.transaction.Transaction;

import java.util.List;

/**
 * Created by hhz on 2017/6/29.
 */
public class GroupDao {

    public static final GroupDao groupDao = new GroupDao();

    private GroupDao(){}

    public static GroupDao getInstance() {
        return groupDao;
    }

    public void saveAll(List<Group> groups) {
//        FlowManager.getModelAdapter(User.class).saveAll(users);
        Delete.table(Group.class);
        DatabaseDefinition database = FlowManager.getDatabase(PartChatDataBase.class);
        FastStoreModelTransaction<Group> fastStoreModelTransaction = FastStoreModelTransaction
                .insertBuilder(FlowManager.getModelAdapter(Group.class))
                .addAll(groups)
                .build();
        Transaction transaction = database.beginTransactionAsync(fastStoreModelTransaction).build();
        transaction.execute();
    }

    /**
     * 根据Id获取数据
     * @param groupId
     * @return
     */
    public Group getGroupById(String groupId) {
        return SQLite.select().from(Group.class)
                .where(Group_Table.groupId.eq(groupId))
                .querySingle();
    }
}
